package com.hddz.acquisition.activity.stock;

import java.util.HashMap;
import java.util.Map;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import com.hddz.acquisition.R;
import com.hddz.acquisition.activity.BaseActivity;
import com.hddz.acquisition.fragment.stock.FragmentGroundingFree1;
import com.hddz.acquisition.fragment.stock.FragmentGroundingScan;
import com.hddz.acquisition.model.JsonRespose;

@ContentView(R.layout.activity_stock_grounding)
public class OutStockActivity extends BaseActivity {
	private final String TAG = "GroundingActivity";
	@ViewInject(R.id.tab_vpager)
	private ViewPager tab_vpager;

	@ViewInject(R.id.lbl_main_title)
	private TextView lbl_main_title;

	@SuppressLint("UseSparseArrays")
	private Map<Integer, Fragment> mCacheFragments = new HashMap<Integer, Fragment>();

	private Fragment mFm = null;
	FragmentGroundingScan fragmentGroundingScan = null;
	FragmentGroundingFree1 fragmentGroundingFree1 = null;
	private Bundle bundle = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		lbl_main_title.setText(R.string.menu_dbck);
		tab_vpager.setAdapter(new MainViewPagerAdapter(getSupportFragmentManager()));
		tab_vpager.setOffscreenPageLimit(2);

		baseSendCommand("1301", "");
	}


	@Override
	public void addResponse(JsonRespose json) {
		if (currentCmd.equals("1301")) {

		} else if (currentCmd.equals("1302")) {
			targetwarehouse = json.response.cTargetWarehouse;
			fragmentGroundingScan.updateResponse(json);
		} else if (currentCmd.equals("1303")) {
			fragmentGroundingScan.updateResponse(json);
			fragmentGroundingFree1.updateList(json.response.ResponseList);
		} else if (currentCmd.equals("1304")) {
			dialogHelper.showAlertDialog("保存成功!", 24);
			finish();
		} else if (currentCmd.equals("1305")) {
			finish();
		}
		fragmentGroundingScan.appandMsg(json.message);
	}

	@Override
	public void addErr(String err) {
		fragmentGroundingScan.appandMsg(err);
	}

	@Override
	public void receiveCode(String code) {
		fragmentGroundingScan.updateBarcode(code);
		if (currentCmd.equals("1301")) {
			baseSendCommand("1302", "", code);
		} else if (currentCmd.equals("1302") || currentCmd.equals("1303")) {
			baseSendCommand("1303", code);
		}
	}

	@Override
	public void backDo() {
		onBack(null);
	}

	@Override
	public void finishDo() {
		onFinish(null);
	}

	private class MainViewPagerAdapter extends FragmentPagerAdapter {

		public MainViewPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return 2;
		}

		@Override
		public Fragment getItem(int position) {
			if (mCacheFragments.containsKey(position)) {
				mFm = mCacheFragments.get(position);
			} else {
				switch (position) {
				case 0:
					fragmentGroundingScan = FragmentGroundingScan.newInstance();
					bundle = new Bundle();
					bundle.putString("Title", "椤圭洰1");
					fragmentGroundingScan.setArguments(bundle);
					mFm = fragmentGroundingScan;
					break;
				case 1:
					fragmentGroundingFree1 = FragmentGroundingFree1.newInstance();
					bundle = new Bundle();
					bundle.putString("Title", "椤圭洰1");
					fragmentGroundingFree1.setArguments(bundle);
					mFm = fragmentGroundingFree1;
					break;
				default:
					break;
				}
				mCacheFragments.put(position, mFm);
			}
			return mFm;
		}
	}



	@Event(R.id.btn_back)
	private void onBack(View view) {
		if (targetwarehouse.equals("")) {
			finish();
		} else {

			dialogHelper.showAlertDialog("是否取消操作?", 24, new OnClickListener() {

				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					baseSendCommand("1305", "");
				}
			});
		}
	}

	@Event(R.id.btn_finish)
	private void onFinish(View view) {
		if (targetwarehouse.equals("")) {
			dialogHelper.showAlertDialog("未扫描架子！", 24);
		} else {
			baseSendCommand("1304", "");
		}
	}

}
